<?php include_once('header.php'); ?>
<?php include_once('left_navigation.php'); ?>
<?php
	include('../classess/class_affiliate_user.php');
?>
<div class="col-lg-12"><!-- Start Middle Content Div-->
	<div class="box">
		<div class="row">
			<div class="col-lg-12" style="margin-top:-20px; text-align:center;">
				<div class="box" style="font-size:16px;">
					<?php echo 'Your Refrence URL is <a href="http://'.$_SERVER['SERVER_NAME'].'/reference_subscribe.php?ref_url='.$_SESSION['user_reference_id'].'" target="_blank" style="text-decoration:none; color:blue; ">http://'.$_SERVER['SERVER_NAME'].'/reference_subscribe.php?ref_url='.$_SESSION['user_reference_id'].'</a>';?>
				</div>
			</div>
			<div class="col-lg-12">
				<div class="box">
					<div class="box-header" data-original-title>
						<h2><i class="fa fa-user"></i><span class="break"></span>Affiliate Users</h2>
						<div class="box-icon">
							<a href="table.html#" class="btn-minimize"><i class="fa fa-chevron-up"></i></a>
							<a href="table.html#" class="btn-close"><i class="fa fa-times"></i></a>
						</div>
					</div>
					<div class="box-content">
						<div class="search_box">
							<div class="fl_left sel_label_search">Search:</div>
							<div class="fl_left" style="width:60%;">									
								<form method="POST" action="<?php echo $_SERVER['PHP_SELF'].'?home=1'; ?>" id="search_form" name="search_form">
									<input type="text" name="search_value" id="search_value" placeholder="Search" value="<?php if(isset($_POST['search_value'])){echo $_POST['search_value']; }else{ echo '';}  if(isset($_GET['srch_value'])){echo $_GET['srch_value']; }else{ echo '';}?>">&nbsp;
									<input type="submit" name="submit_search" id="submit_search" class="btn btn-blue srch_btn" value="Search">
								</form>

							</div>
							<div class="clear_fix"></div>
						</div>
						<table class="table table-striped table-bordered">
						  <thead>
							  <tr>
								  <th>S. No.</th>
								  <th>User Name</th>
								  <th>Email</th>
								  <th>Phone</th>
								  <th>City</th>
								  <th>State</th>
								  <th>Country</th>
								  <th>Commission Percentage</th>
								  <th>Commission</th>
							  </tr>
						  </thead>   
						  <tbody>
							<?php
								$rec_limit = 25;

								$condition_search = '';
								$value = '';
								if(isset($_POST['search_value'])){										
									$value = trim($_POST['search_value']);
									
									if(!empty($value)){
										$cond_where = 'AND ';
										$condition_search = $cond_where." (username like '%".$value."%' OR email like '%".$value."%' OR phone like '%".$value."%' OR city like '%".$value."%' OR state like '%".$value."%' OR country like '%".$value."%')";
									}
								}

								if(isset($_GET['srch_value'])){	
									$value = $_GET['srch_value'];
									
									if(!empty($value)){
										$cond_where = 'AND ';

										$condition_search = $cond_where." (username like '%".$value."%' OR email like '%".$value."%' OR phone like '%".$value."%' OR city like '%".$value."%' OR state like '%".$value."%' OR country like '%".$value."%')";
									}
								}

								$select_count_course = "SELECT count(*) FROM tbl_user as a, tbl_user_details as b WHERE a.id='".$_GET['uid']."' AND b.user_id=a.id ".$condition_search;
								$result_count_course = mysql_query($select_count_course);

								$row = mysql_fetch_array($result_count_course);
								$rec_count = $row[0];

								if( isset($_GET['page']) )
								{
								   $page = $_GET['page'] + 1;
								   $offset = $rec_limit * $page ;
								}
								else
								{
								   $page = 0;
								   $offset = 0;
								}

								$serial_number = $page*$rec_limit;
								
								$left_rec = $rec_count - ($page * $rec_limit);

								$select_trainer = "SELECT a.*, b.*, b.id as dtl_id FROM tbl_user as a, tbl_user_details as b WHERE a.id='".$_GET['uid']."' AND b.user_id=a.id ".$condition_search." ORDER BY a.id DESC LIMIT ".$offset.", ".$rec_limit;
								$query_trainer = mysql_query($select_trainer) or die(mysql_error());
								
								$i = 1;
								if(mysql_num_rows($query_trainer)>0){
									while($row = mysql_fetch_array($query_trainer)){
										$serial_number++;
										$chk_id ='';
										$comm_per ='';
										$comm ='';
										$select_trainer_dtl = "SELECT a.*, b.*, b.id as comm_id FROM tbl_user_details as a, tbl_commission as b WHERE b.trainee_detail_id='".$row['dtl_id']."' AND b.user_admin_id='a_".$_SESSION['id']."' ORDER BY a.id DESC LIMIT ".$offset.", ".$rec_limit;
										$query_trainer_dtl = mysql_query($select_trainer_dtl) or die(mysql_error());
										if(mysql_num_rows($query_trainer_dtl)>0){
											while($row_type = mysql_fetch_array($query_trainer_dtl)){	
												$chk_id = $row_type['comm_id'];
												$comm_per = $row_type['commission_percentage'];
												$comm = $row_type['commission'];
											}
										}
										echo '
											<tr>
												<td>'.$serial_number.'</td>
												<td>'.$row['username'].'</td>
												<td>'.$row['email'].'</td>
												<td>'.$row['phone'].'</td>
												<td>'.$row['city'].'</td>
												<td>'.$row['state'].'</td>
												<td>'.$row['country'].'</td>';
												if($chk_id){
													echo '<td>'.$comm_per.'</td>
													<td>'.$comm.'</td>';
												}else{
													echo '<td>No Commission Percentage</td>
													<td>No Commission</td>';
												}
											echo '</tr>
										';
										$i++;
									}
								}else{
									echo '<tr>
										<td colspan="9" style="text-align:center;">No Records Found</td>
									</tr>';
								}

							?>						
						  </tbody>
					  </table>  
					  <?php		
						echo '<ul class="pagination">';
						if($rec_count>$rec_limit){
							if( $page > 0 && $left_rec > $rec_limit)
							{
							   $last = $page - 2;
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?home=1&page='.$last.'&srch_value='.$value.'&uid='.$_GET['uid'].'" class="anc_click">Prev</a></li> ';
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?home=1&page='.$page.'&srch_value='.$value.'&uid='.$_GET['uid'].'" class="anc_click">Next</a></li>';
							}
							else if( $page == 0 )
							{
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?home=1&page='.$page.'&srch_value='.$value.'&uid='.$_GET['uid'].'" class="anc_click">Next</a></li>';
							}
							else if( $left_rec < $rec_limit || $left_rec == $rec_limit )
							{
							   $last = $page - 2;
							   echo '<li><a href="'.$_SERVER["PHP_SELF"].'?home=1&page='.$last.'&srch_value='.$value.'&uid='.$_GET['uid'].'" class="anc_click">Prev</a></li>';
							}
						}
						echo '</ul>';
						?>
					</div>
				</div>
			</div><!--/col-->

		</div><!--/row-->

	</div><!-- End Box Div-->
</div><!-- End Middle Content Div-->
<?php include_once('footer.php'); ?>
<script>
	$ = jQuery.noConflict();
	$(document).ready(function(){	
		$('.close').click(function(){
			$('.alert-info').addClass('hide');
		});

		$('#submit_course_search').click(function(){
			document.search_course_form.submit()
		});
	});
		
	
</script>